/*
 * Copyright 2002-2016 the Beijing Brain Technology co.,LTD http://www.bris.cn/
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bris.cert.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bris.niosocket.annotation.Path;
import com.bris.niosocket.annotation.SocketController;
import com.bris.niosocket.message.impl.RequestMessage;
import com.bris.niosocket.message.impl.ResponseMessage;
import com.bris.sign.SignVerifyUtil;

/**
 * TODO 描述该类型的作用</br>
 * 2016年2月24日</br>
 * @author xunaidong
 * @since 1.0.0
 */
@SocketController
public class CommonsContrlloer {
	
	private final Logger log = LoggerFactory.getLogger(CommonsContrlloer.class);
	
	@Path("RANDOM")
	public void generNumber(RequestMessage request,ResponseMessage response){
		log.debug("业务处理中!");
		response.pushUnique("RANDOM", "123456789");
		log.debug("业务处理完毕!");
		
	}
	@Path("P7SIGNMESSAGEDETACHWITHRSA1024")
	public void sign(RequestMessage request,ResponseMessage response){
		log.debug("业务处理中!");
		try {
			if(!SignVerifyUtil.p7VerifyMessageDetachWithRSA(request.getUniqueValue("SIGNORIGINAL"), request.getUniqueValue("SIGNDATA"))){
				response.setErrorCode("0001");
				response.setErrorMsg("验签失败!");
			}
		} catch (Exception e) {
			log.error("验签失败",e);
			response.setErrorCode("0001");
			response.setErrorMsg("验签失败!");
		}
		log.debug("业务处理完毕!");
		
	}
	@Path("P7SIGNMESSAGEDETACHWITHSM2")
	public void sm2Sign(RequestMessage request,ResponseMessage response){
		log.debug("业务处理中!");
		try {
			if(!SignVerifyUtil.p7VerifyMessageDetachWithSM2(request.getUniqueValue("SIGNORIGINAL"), request.getUniqueValue("SIGNDATA"))){
				response.setErrorCode("0001");
				response.setErrorMsg("验签失败!");
			}
		} catch (Exception e) {
			log.error("验签失败",e);
			response.setErrorCode("0001");
			response.setErrorMsg("验签失败!");
		}
		log.debug("业务处理完毕!");
		
	}

}
